package com.sniper.web.freemarker;

import com.sniper.data.BaseData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.format.annotation.DateTimeFormat;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ViewProjectImport {


    static final Logger logger = LoggerFactory.getLogger(ViewProjectImport.class);
    /**
     * 占位符前缀
     */
    public static final String NO_FIELD_PREFIX = "noField";

    public static String getFiledValue(Object o, String field) {

        if (o == null || field == null) {
            return "";
        }

        if (field.startsWith(NO_FIELD_PREFIX)) {
            return "";
        }


        if (field.indexOf("|") > 0) {
            String[] fields = field.split("\\|");
            StringBuilder buffer = new StringBuilder();
            for (String field1 : fields) {
                buffer.append(FieldValue(o, field1));
            }
            return buffer.toString();
        } else {
            return FieldValue(o, field);
        }

    }

    private static String FieldValue(Object objectSource, String field) {
        if (objectSource == null || field == null) {
            return "";
        }

        try {
            Field fieldObject = objectSource.getClass().getDeclaredField(field);

            fieldObject.setAccessible(true);
            Class type = fieldObject.getType();
            if (type.equals(String.class)) {
                return (String) fieldObject.get(objectSource);
            } else if (type.equals(Integer.class) || type.equals(int.class)) {
                return String.valueOf(fieldObject.getInt(objectSource));
            } else if (type.equals(Date.class)) {
                Date resultObject = (Date) fieldObject.get(objectSource);
                SimpleDateFormat dateFormat = new SimpleDateFormat(BaseData.DATE_TIME_FORMAT);
                if (fieldObject.isAnnotationPresent(DateTimeFormat.class)) {
                    DateTimeFormat dateTimeFormat = fieldObject.getAnnotation(DateTimeFormat.class);
                    dateFormat = new SimpleDateFormat(dateTimeFormat.pattern());
                }
                if (resultObject != null) {
                    return dateFormat.format(resultObject);
                }
            } else if (type.equals(Boolean.class)) {
                Boolean b = fieldObject.getBoolean(objectSource);
                return String.valueOf(b);
            } else if (type.equals(Long.class) || type.equals(long.class)) {
                return String.valueOf(fieldObject.getLong(objectSource));
            }

        } catch (NoSuchFieldException | IllegalAccessException e) {
            logger.error(e.getMessage());
            return "";
        }

        return "";
    }

    public static String getFiledValue(Object project, String filed, boolean target) {

        if (project == null || filed == null) {
            return "";
        }
        filed = filed.substring(0, 1).toUpperCase() + filed.substring(1);
        filed = "get" + filed;
        String result = "";

        try {
            Method method = project.getClass().getMethod(filed);
            Type type = method.getGenericReturnType();
            Object object = method.invoke(project);
            if (type.equals(String.class)) {
                result = (String) method.invoke(project);
                if (target && filed.equals("getProjectName")) {
                    result = "<a href=\"admin/admin-projects/updateSign?id="
                            + "\" target=\"_blank\">" + result + "</a>";
                }
            } else if (type.equals(Integer.class)) {
                if (object != null) {
                    result = String.valueOf(object);
                }
            } else if (type.equals(Date.class)) {
                Date result2 = (Date) method.invoke(project);
                DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                result = dateFormat.format(result2);
            } else if (type.equals(Boolean.class)) {
                boolean result4 = (boolean) method.invoke(project);
                result = String.valueOf(result4);
            }

            return result;

        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;

    }

    public static void main(String[] args) {
        String a = "as|asdsa|aaa";
        System.out.println(a.indexOf("|"));
        if (a.indexOf("|") > 0) {
            System.out.println(a.split("\\|").length);
        }
    }
}
